package com.cssw.fyzb.tps.service.pay;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.tps.config.pay.WxPay;
import com.cssw.fyzb.tps.util.IpKit;
import com.ijpay.core.enums.SignType;
import com.ijpay.core.enums.TradeType;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.model.OrderQueryModel;
import com.ijpay.wxpay.model.UnifiedOrderModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: chenchao
 * @Date: 2020/7/14 22:11
 */

@Service
public class WxPayServiceImpl implements WxPayService {

    private static final Logger logger = LoggerFactory.getLogger(WxPayServiceImpl.class);


    @Autowired
    private WxPay wxPay;

    @Override
    public Map appPay(String tradeNo, String body, String subject, String amount) {

        WxPayApiConfig wxPayApiConfig = wxPay.getApiConfig();

        Map<String, String> params = UnifiedOrderModel
                .builder()
                .appid(wxPayApiConfig.getSlAppId())
                .mch_id(wxPayApiConfig.getSlMchId())
                .sub_mch_id(wxPayApiConfig.getMchId())
                .sub_appid(wxPayApiConfig.getAppId())
                .nonce_str(WxPayKit.generateStr())
                .body(body)
                .attach(subject)
                .out_trade_no(tradeNo)//
                .total_fee(amount)
                .spbill_create_ip(IpKit.getLocalIP())
                .notify_url(wxPay.getNotifyUrl())
                .limit_pay("no_credit")//关闭信用卡
                .trade_type(TradeType.APP.getTradeType())
                .build()
                .createSign(wxPayApiConfig.getApiKey(), SignType.HMACSHA256);


        String resultStr = WxPayApi.pushOrder(params);
        Map<String, String> result = WxPayKit.xmlToMap(resultStr);

        String returnCode = result.get("return_code");
        if (!WxPayKit.codeIsOk(returnCode)) {
            return null;
        }

        String prepayId = result.get("prepay_id");

        Map<String, String> packageParams = WxPayKit.appPrepayIdCreateSign(wxPayApiConfig.getAppId(), wxPayApiConfig.getMchId(), prepayId,
                wxPayApiConfig.getApiKey(), SignType.HMACSHA256);
        return packageParams;

    }


    @Override
    public Map<String, String> orderQuery(String tradeNo) {

        WxPayApiConfig wxPayApiConfig = wxPay.getApiConfig();

        Map<String, String> queryParam = OrderQueryModel.builder()
                .appid(wxPayApiConfig.getSlAppId())
                .mch_id(wxPayApiConfig.getSlMchId())
                .sub_mch_id(wxPayApiConfig.getMchId())
                .sub_appid(wxPayApiConfig.getAppId())
                .out_trade_no(tradeNo)
                .nonce_str(WxPayKit.generateStr())
                .build()
                .createSign(wxPayApiConfig.getApiKey(), SignType.HMACSHA256);
        String resultStr = WxPayApi.orderQuery(queryParam);
        Map<String, String> result = WxPayKit.xmlToMap(resultStr);
        return result;
    }

    @Override
    public boolean verifyNotify(Map<String, String> params) {

        logger.info("ali verifyNotify: {} ", JSON.toJSONString(params));

        return WxPayKit.verifyNotify(params, wxPay.getApiKey(), SignType.HMACSHA256);
    }

}
